﻿using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
namespace System {
    public class SmtpClientHelper {
        /// <summary>
        /// 邮件服务器地址
        /// </summary>
        public string MailServer { get; set; } = "smtp.163.com";
        /// <summary>
        /// 用户名
        /// </summary>
        public string MailUserName { get; set; } = "minguiluo@163.com";
        /// <summary>
        /// 密码
        /// </summary>
        public string MailPassword { get; set; } = "TPSDOUOTAVMKSESD";
        /// <summary>
        /// 名称
        /// </summary>
        public string MailName { get; set; } = "minguiluo@163.com";
        /// <summary>
        /// 同步发送邮件
        /// </summary>
        /// <param name="to">收件人邮箱地址</param>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="encoding">编码</param>
        /// <param name="isBodyHtml">是否Html</param>
        /// <param name="enableSsl">是否SSL加密连接</param>
        /// <returns>是否成功</returns>
        public bool Send(string to,string subject,string body,string encoding = "UTF-8",bool isBodyHtml = true,bool enableSsl = false) {
            try {
                MailMessage message = new MailMessage();
                // 接收人邮箱地址
                message.To.Add(new MailAddress(to));
                message.From = new MailAddress(MailUserName,MailName);
                message.BodyEncoding = Encoding.GetEncoding(encoding);
                message.Body = body;
                //GB2312
                message.SubjectEncoding = Encoding.GetEncoding(encoding);
                message.Subject = subject;
                message.IsBodyHtml = isBodyHtml;
                SmtpClient smtpclient = new SmtpClient(MailServer,25);
                smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName,MailPassword);
                //SSL连接
                smtpclient.EnableSsl = enableSsl;
                smtpclient.Send(message);
                return true;
            } catch(Exception) {
                throw;
            }
        }
        /// <summary>
        /// 异步发送邮件 独立线程
        /// </summary>
        /// <param name="to">邮件接收人</param>
        /// <param name="title">邮件标题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="port">端口号</param>
        /// <returns></returns>
        public void SendEmailAsync(string to,string title,string body,int port = 25) {
            new Thread(new ThreadStart(delegate () {
                try {
                    SmtpClient smtp = new SmtpClient();
                    //邮箱的smtp地址
                    smtp.Host = MailServer;
                    //端口号
                    smtp.Port = port;
                    //构建发件人的身份凭据类
                    smtp.Credentials = new NetworkCredential(MailUserName,MailPassword);
                    //构建消息类
                    MailMessage objMailMessage = new MailMessage();
                    //设置优先级
                    objMailMessage.Priority = MailPriority.High;
                    //消息发送人
                    objMailMessage.From = new MailAddress(MailUserName,MailName,System.Text.Encoding.UTF8);
                    //收件人
                    objMailMessage.To.Add(to);
                    //标题
                    objMailMessage.Subject = title.Trim();
                    //标题字符编码
                    objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
                    //正文
                    objMailMessage.Body = body.Trim();
                    objMailMessage.IsBodyHtml = true;
                    //内容字符编码
                    objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                    //发送
                    smtp.Send(objMailMessage);
                } catch(SmtpException ex) {
                    Logger.LogError("SendEmail:",ex);
                }
            })).Start();
        }
        /// <summary>
        /// 发送邮件方法
        /// </summary>
        /// <param name="mailTo">接收人邮件</param>
        /// <param name="mailTitle">发送邮件标题</param>
        /// <param name="mailContent">发送邮件内容</param>
        /// <returns></returns>
        public bool SendEmail(string mailTo,string mailTitle,string mailContent) {
            //设置发送方邮件信息，例如：qq邮箱
            string stmpServer = MailServer;//smtp服务器地址
            string mailAccount = MailUserName;//邮箱账号
            string pwd = MailPassword;//邮箱密码（qq邮箱此处使用授权码，其他邮箱见邮箱规定使用的是邮箱密码还是授权码）
            //邮件服务设置
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            smtpClient.Host = stmpServer;//指定发送方SMTP服务器
            smtpClient.EnableSsl = true;//使用安全加密连接
            smtpClient.UseDefaultCredentials = true;//不和请求一起发送
            smtpClient.Credentials = new NetworkCredential(mailAccount,pwd);//设置发送账号密码
            MailMessage mailMessage = new MailMessage(mailAccount,mailTo);//实例化邮件信息实体并设置发送方和接收方
            //mailMessage.CC.Add("1114800402@qq.com");
            mailMessage.Subject = mailTitle;//设置发送邮件得标题
            mailMessage.Body = mailContent;//设置发送邮件内容
            mailMessage.BodyEncoding = Encoding.UTF8;//设置发送邮件得编码
            mailMessage.IsBodyHtml = false;//设置标题是否为HTML格式
            mailMessage.Priority = MailPriority.Normal;//设置邮件发送优先级
            try {
                smtpClient.Send(mailMessage);//发送邮件
                return true;
            } catch(SmtpException ex) {
                Logger.LogError("SendEmail:",ex);
                return false;
            }
        }
    }
}